package cc.fs.kotlin.ui.demo

import android.os.Bundle
import android.os.Message
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.widget.TextView
import cc.fs.kotlin.R
import cc.fs.kotlin.ui.adapter.DataAdapter
import cc.fs.kotlin.ui.activity.helper.BaseActivity
import cc.fs.kotlin.utils.SwipeHelper
import cc.fs.kotlin.utils.bindView
import java.util.*

/**
 * demo swipeHelper使用方式测试
 * Created by fostion on 2/18/16.
 */
class RecycleViewDemo : BaseActivity() {

    val recycleView: RecyclerView by bindView(R.id.template_rv_list)
    val swipeRefreshLayout: SwipeRefreshLayout by bindView(R.id.template_sr_layout)
    val tipView: TextView by bindView(R.id.template_tv_tip)
    val list = ArrayList<String>()
    val adapter: DataAdapter = DataAdapter(list)
    var swipeHelper:SwipeHelper ?= null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_demo_recycleview)

        initView()
        initData()
    }

    private fun initView() {
        recycleView.layoutManager = LinearLayoutManager(this@RecycleViewDemo)
        recycleView.adapter = adapter

        swipeHelper = SwipeHelper(swipeRefreshLayout, recycleView, object : SwipeHelper.SwipeListener {
            override fun onRefresh() {
                Log.e("---", " 刷新信息 ")
                runDelayed({
                    swipeHelper?.finishThisLoading()
                },2000)
            }

            override fun onLoadMore() {
                Log.e("---", " 加载更多信息 ")
                runDelayed({
                    swipeHelper?.finishThisLoading()
                    swipeHelper?.setIsCanLoadMore(false)
                },2000)
            }
        })
    }

    private fun initData() {
        list.add("0")
        list.add("1")
        list.add("2")
        list.add("3")
        list.add("4")
        list.add("5")
        list.add("6")
        list.add("7")
        list.add("8")
        list.add("9")
        list.add("10")
        list.add("11")
        list.add("12")
        list.add("13")
        list.add("14")
        adapter.notifyDataSetChanged()
    }

    override fun handler(msg: Message) {
    }
}